package com.hyt.it.ogt.kq.main.controller.gov;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.common.config.exception.KqException;
import com.hyt.it.ogt.kq.service.gov.model.param.PaperDeleteParam;
import com.hyt.it.ogt.kq.service.gov.model.param.PaperParam;
import com.hyt.it.ogt.kq.service.gov.model.vo.PaperVO;
import com.hyt.it.ogt.kq.service.gov.service.IPaperService;
import com.hyt.model.PageParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * <p>
 * 试卷管理
 * </p>
 *
 * @author liuq
 * @since 2021-04-27
 */
@RestController
@Api(tags = "109.试卷接口", value = "试卷接口")
@ApiSort(value = 109)
@RequestMapping("/kw/gov/paper")
@Slf4j
public class PaperController {
	
	@Resource
	private IPaperService iPaperService;

	@ApiOperation(
		value = "9.1 查询试卷管理分页数据",
		notes = "30000：成功"
	)
	@ApiOperationSort(value = 1)
	@RequestMapping(value = "/get/page", method = RequestMethod.GET)
	public ApiResponse<Page<PaperVO>> page(PageParam<PaperVO> pageParam, String taskId) {
		log.info("# 查询试卷管理分页数据参数: {}", taskId);
		try {
			Page<PaperVO> page = iPaperService.paperPage(pageParam, taskId);
			return ApiResponse.<Page<PaperVO>>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(page)
					.build();
		} catch (Exception e) {
			log.error("# 查询考试任务分页数据异常:", e);
			return ApiResponse.<Page<PaperVO>>builder()
					.code(ResponseCode.ERROR_TASK_QUERY_PAGE.getCode())
					.build();
		}
	}
	
	@ApiOperation(
		value = "9.2 导入试卷",
		notes = "30000：成功;" +
				"30023：不是合法的zip文件;" +
				"33101：导入试卷失败;" +
				"31611:批次科目考卷id不存在;" +
				"31610:试卷保存失败;" +
				"31609:试卷包内容异常;" +
				"31608:考卷科目不匹配;" +
				"31607:批次科目中考卷已存在;" +
				"31601:导入试卷失败;"+
				"31617:批次科目试卷id不能为空;"
	)
	@ApiOperationSort(value = 2)
	@RequestMapping(value = "/import/paper", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ApiResponse<Object> importPaper(@RequestPart(value = "file") MultipartFile file,
										   @RequestParam String taskId, @RequestParam String timeSubjectId) {
		log.info("# 导入试卷参数: {}, {}", taskId, timeSubjectId);
		try {
			iPaperService.importPaper(file, taskId, timeSubjectId);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.build();
		} catch (KqException k) {
			//业务异常
			log.error("# 导入试卷业务异常: {}", k.getErrMsg());
			return ApiResponse.builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			//其他异常
			log.error("# 导入试卷异常: ", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_PAPER_IMPORT.getCode())
					.build();
		}
	}
	
	@ApiOperation(
		value = "9.3 导入授权码",
		notes = "30000：成功;" +
				"31602:导入授权码失败;" +
				"31612:读取授权码包内容异常;" +
				"31613:试卷编号不存在或该编号的试卷还未导入;" +
				"31614:该试卷的授权码包不能重复导入;" +
				"31615:授权码更新失败;" +
				"31616:试卷id不能为空;"
	)
	@ApiOperationSort(value = 3)
	@RequestMapping(value = "/import/authcode", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ApiResponse<Object> importAuthCode(@RequestPart(value = "file") MultipartFile file,
											  @RequestParam String paperId) {
		log.info("# 导入授权码入参: {}", paperId);
		try {
			iPaperService.importAuthCode(file, paperId);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.build();
		} catch (KqException k) {
			//业务异常
			log.error("# 导入授权码业务异常: {}", k.getErrMsg());
			return ApiResponse.builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			//其他异常
			log.error("# 导入授权码异常", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_PAPER_AUTH_IMPORT.getCode())
					.build();
		}
	}
	


	@ApiOperation(
		value = "9.4 启用和停用试卷",
		notes = "30000：成功;" +
				"31604:更新试卷启用状态异常;"
	)
	@ApiOperationSort(value = 4)
	@PutMapping("/paper/status")
	public ApiResponse<Object> status(@RequestBody PaperParam paperParam) {
		log.info("# 停启用试卷参数: {}", JSON.toJSONString(paperParam));
		try {
			iPaperService.updatePaperStatus(paperParam);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.build();
		} catch (KqException e) {
			return ApiResponse.builder()
					.code(e.getErrCode())
					.data(e.getErrMsg())
					.build();
		} catch (Exception e) {
			log.error("# 停启用试卷异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_PAPER_UPDATE_STATUS.getCode())
					.build();
		}
	}
	
	@ApiOperation(
		value = "9.5 删除试卷",
		notes = "30000：成功;" +
				"31606:删除试失败"
	)
	@ApiOperationSort(value = 5)
	@DeleteMapping("/paper/delete")
	public ApiResponse<Object> delete(@RequestBody PaperDeleteParam paperDeleteParam) {
		log.info("# 删除试卷参数: {}", JSON.toJSONString(paperDeleteParam));
		try {
			iPaperService.deleteByTimeSubject(paperDeleteParam);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.build();
		} catch (KqException k) {
			log.error("# 删除试卷业务异常: {}", k.getErrMsg());
			return ApiResponse.builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 删除试卷异常", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_TASK_DELETE_PAPER.getCode())
					.build();
		}
	}



	@ApiOperation(
			value = "9.6 查询模拟试卷管理分页数据",
			notes = "30000:成功;" +
					"43090601:查询模拟试卷管理分页数据异常;"
	)
	@ApiOperationSort(value = 6)
	@RequestMapping(value = "/get/mock", method = RequestMethod.GET)
	public ApiResponse<Page<PaperVO>> mockPaperPage(PageParam<PaperVO> pageParam, String taskId) {
		log.info("# 查询模拟试卷管理分页数据参数: {}", taskId);
		try {
			Page<PaperVO> page = iPaperService.mockPaperPage(pageParam, taskId);
			return ApiResponse.<Page<PaperVO>>builder()
					.code(ResponseCode.SUCCESS.getCode())
					.data(page)
					.build();
		} catch (Exception e) {
			log.error("# 查询模拟试卷管理分页数据异常", e);
			return ApiResponse.<Page<PaperVO>>builder()
					.code(ResponseCode.ERROR_MOCK_PAPER_GET_EXCEPTION.getCode())
					.build();
		}
	}


	@ApiOperation(
			value = "9.7 删除模拟试卷",
			notes = "30000：成功;" +
					"31619:删除模拟试卷异常;"
	)
	@ApiOperationSort(value = 7)
	@DeleteMapping(value = "/mock/paper/delete")
	public ApiResponse<Object> deleteMockPaper(@RequestBody PaperDeleteParam paperDeleteParam) {
		log.info("# 删除模拟试卷参数: {}", JSON.toJSONString(paperDeleteParam));
		try {
			iPaperService.deleteMockPaperByTimeSubject(paperDeleteParam);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.build();
		} catch (KqException k) {
			log.error("# 删除模拟试卷业务异常: {}", k.getErrMsg());
			return ApiResponse.builder()
					.code(k.getErrCode())
					.build();
		} catch (Exception e) {
			log.error("# 删除模拟试卷数据异常:", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_MOCK_PAPER_REMOVE_EXCEPTION.getCode())
					.build();
		}
	}

	@ApiOperation(
			value = "9.8 导入模拟试卷",
			notes = "30000：成功;" +
					"30023：不是合法的zip文件;" +
					"33101：导入试卷失败;" +
					"31610:试卷保存失败;" +
					"31609:试卷包内容异常;" +
					"31622:任务id不能为空;" +
					"31623:任务中模拟考卷已经存在;" +
					"31624:科目编号不存在;" +
					"31625:任务id不存在;"
	)
	@ApiOperationSort(value = 8)
	@RequestMapping(value = "/import/mock/paper", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
	public ApiResponse<Object> importMockPaper(@RequestPart(value = "file") MultipartFile file,
											   @RequestParam String taskId, @RequestParam String timeSubjectId) {
		log.info("# 导入模拟试卷入参: {}, {}", taskId, timeSubjectId);
		try {
			iPaperService.importMockPaper(file, taskId, timeSubjectId);
			return ApiResponse.builder()
					.code(ResponseCode.SUCCESS.getCode())
					.build();
		} catch (KqException k) {
			//业务异常
			log.error("# 导入模拟试卷业务异常: {}", k.getErrMsg());
			return ApiResponse.builder()
					.code(k.getErrCode())
					.data(k.getMessage())
					.build();
		} catch (Exception e) {
			//其他异常
			log.error("# 导入模拟试卷异常: ", e);
			return ApiResponse.builder()
					.code(ResponseCode.ERROR_PAPER_IMPORT.getCode())
					.build();
		}
	}

}
